package com.witframework.remote;

import java.io.StringWriter;
import java.util.Collection;

import javax.xml.transform.stream.StreamResult;

import org.milyn.Smooks;
import org.milyn.payload.JavaSource;

public class Java2Xml {
	static Smooks smooks = new Smooks();
	public static String toXml(Object object){
		if (object instanceof Collection){
			return collectionToXml((Collection)object);
		}else{
			return objectToXml(object);
		}
	}
	private static String objectToXml(Object object){
	    StringWriter writer = new StringWriter(10240);
	    smooks.filter(new JavaSource(object), new StreamResult(writer));
        return writer.toString();
	}
	private static String collectionToXml(Collection objects){
		String root="<"+objects.getClass().getName()+">";
		for(Object object:objects){
			root+=objectToXml(object);
		}
		return root+="</"+objects.getClass().getName()+">";
	}

}
